package com.study.rabbit.subscibe;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @description ProducerTest
 * @author TDW
 * @date 2018年10月29日
 *
 */
public class PublishTest {

	private static RabbitTemplate rabbitTemplate;
	
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("MQContext-publish.xml");
		rabbitTemplate = (RabbitTemplate) ac.getBean("amqpTemplate");
		
		
		String opType = "1000";
		String msg = "hahahahhahaa";
		System.out.println("op_type="+opType+",msg="+msg);
		MessageProperties properties = new MessageProperties();
		properties.setHeader("op_type", opType);
		Message message = new Message(msg.getBytes(),properties);
		try{
			rabbitTemplate.send("fanout-exchange", "*", message);
			System.out.println("消息发送成功");
		
			rabbitTemplate.send("broadcastABCD",message);
			Thread.sleep(10000);//等订阅者启动后再发布一条消息
			rabbitTemplate.send("broadcastABCD",message);
			System.out.println("消息发送成功");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}
